<template>
  <el-card style="max-width: 400px;" :class="{ active: tabStore.assembly_type === props.type }" shadow="never">
    <div style="height: 80px; display: flex; align-items: center; justify-content: space-between;"
      v-if="props.index !== '-1'">
      <div class=" card" @click="resetSelect">
        <div class="text">{{ props.type }} </div>
        <div>
          <el-image style="width: 60px; height: 60px" :src="props.data?.img_url_arr[0]" fit="cover" />
        </div>
        <div class="title" v-if="props.type == 'CPU'">{{ props.data?.manufacturers }}-{{ props.data?.series
          }}-{{ props.data?.code_name }}</div>

        <div class="title" v-if="props.type == 'GPU'">{{ props.data?.codeName }}</div>

        <div class="title" v-if="props.type == '主板'">{{ props.data?.series }}</div>

        <div class="title" v-if="props.type == '硬盘'">{{ props.data?.manufacturers }}</div>

        <div class="title" v-if="props.type == '固态硬盘'">{{ props.data?.manufacturers }}</div>
        <div class="title" v-if="props.type == '内存'">{{ props.data?.manufacturers }}</div>
        <div class="price">￥ {{ props.data?.price }}</div>
      </div>

      <el-icon @click="resetData" style="cursor: pointer;margin-left: 5px;">
        <CloseBold />
      </el-icon>


    </div>
    <div class="btn" v-else>
      <el-button @click="addInfo" type="primary" plain>
        添加 {{ type }}
      </el-button>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { useTabStore } from '@/store/useTabStore';
import { CloseBold } from '@element-plus/icons-vue'
import { useAddAssStore } from '@/store/useAddAssStore'
import { useComAssStore } from '@/store/useComAssStore';
const addAssStore = useAddAssStore();
const tabStore = useTabStore();
const comAssStore = useComAssStore();
const props = defineProps({
  type: String,
  index: String,
  data: Object
})

const resetSelect = () => {
  tabStore.setAssemblyType(props.type);
  comAssStore.clearCompare()
};

const resetData = () => {

  // console.log(props.type);

  //清空数据
  const typeHandlers = {
    "CPU": addAssStore.setCpuInfo,
    "GPU": addAssStore.setGpuInfo,
    "主板": addAssStore.setMainboardInfo,
    "内存": addAssStore.setMemoryInfo,
    "固态硬盘": addAssStore.setSsdInfo,
    "硬盘": addAssStore.setHddInfo
  };

  const handler = typeHandlers[props.type];
  if (handler) {
    handler({}, "-1");
    addAssStore.setTotal();
  }

};

const addInfo = () => {
  tabStore.setAssemblyType(props.type);
  comAssStore.clearCompare()
};
</script>

<style scoped lang="scss">
.active {
  border: 1px solid #e709fc;
}

::v-deep .el-card__body {
  padding: 0 5px;
}

.btn {
  height: 80px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.card {
  height: max-content;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-around;
  cursor: pointer;



  .text {
    font-size: 12px;
    font-weight: 600;
    margin-right: 5px;
  }

  .title {
    margin-left: 5px;
    font-size: 12px;
    font-weight: 600;
    width: 180px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;

  }

  .price {
    font-size: 12px;
    font-weight: bold;
    color: #ff0000;
  }
}
</style>